﻿module Sustainalytics.LRTMTest {

    import LRTM = Sustainalytics.LRTM;

    // LRTM controller
    export class LongRunningTaskManagerTestController {

        constructor(
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private LongRunningTaskManagerService: LRTM.LongRunningTaskManagerService) {

            this.$scope['lrtmtest'] = this;
        }

        public addTaskDelayed(): void {
            var ctx = this;
            var promise = ctx.LongRunningTaskManagerService.runTask('Delayed task ' + new Date().getSeconds(), '{ myData: \'This is my data\' }', 11000, false,
                (id, newStatus, oldStatus, taskInstance) => ctx.statusChangedCallback(id, newStatus, oldStatus, taskInstance));
        }

        public addTaskDownload(): void {
            var ctx = this;
            
            var data =  { myData: 'This is my data' };

            var promise = ctx.LongRunningTaskManagerService.runTask('Download task ' + new Date().getSeconds(), JSON.stringify(data), 12000, true,
                (id, newStatus, oldStatus, taskInstance) => ctx.statusChangedCallback(id, newStatus, oldStatus, taskInstance));
        }

        public addTaskUpload(): void {
            var ctx = this;

            var files = $('#inputFile').get(0)['files'];
            var promise = ctx.LongRunningTaskManagerService.runTaskWithUpload('Upload task ' + new Date().getSeconds(), '{ myData: \'This is my data\' }', 10000, files, null, true,
                (id, newStatus, oldStatus, taskInstance) => ctx.statusChangedCallback(id, newStatus, oldStatus, taskInstance));
        }

        public addTaskError(): void {
            var ctx = this;
            var promise = ctx.LongRunningTaskManagerService.runTask('Error task ' + new Date().getSeconds(), '{ myData: \'This is my data\' }', 13000, true,
                (id, newStatus, oldStatus, taskInstance) => ctx.statusChangedCallback(id, newStatus, oldStatus, taskInstance));
        }


        private statusChangedCallback(taskInstanceId: string, newStatus: LRTM.LongRunningTaskInstanceStatusEnum, oldStatus: LRTM.LongRunningTaskInstanceStatusEnum, taskInstance: LRTM.LongRunningTaskInstance): void {
            var ctx = this;

            // display status
            var newStatusName = this.getStatusName(newStatus);
            var oldStatusName = this.getStatusName(oldStatus);
            $('#eventList').prepend('<li style="margin-top: 7px;"> Instance: ' + taskInstanceId + ' from ' + oldStatusName + ' to ' + newStatusName + '. </li>');

            // display result
            if (newStatus == LRTM.LongRunningTaskInstanceStatusEnum.Finished) {
                var promise = ctx.LongRunningTaskManagerService.getResult(taskInstanceId, false);
                promise.then(function (res) {
                    var resStr = JSON.stringify(res['data']);
                    $('#eventList').prepend('<li> Instance: ' + taskInstanceId + ' has result: ' + resStr + ' </li>');
                });
            }

            if (taskInstance.status.taskStatus.errors && taskInstance.status.taskStatus.errors.length > 0) {
                console.log('Task instance ' + taskInstanceId + ' completed with errors:');
                console.log(taskInstance.status.taskStatus.errors);
            }
            else {
                console.log('Task instance ' + taskInstanceId + ' completed with no errors.');
            }
        }

        private getStatusName(status: LRTM.LongRunningTaskInstanceStatusEnum) {
            switch (status) {
                case LRTM.LongRunningTaskInstanceStatusEnum.Cancelled:
                    return "Cancelled";
                case LRTM.LongRunningTaskInstanceStatusEnum.Failed:
                    return "Failed";
                case LRTM.LongRunningTaskInstanceStatusEnum.Finished:
                    return "Finished";
                case LRTM.LongRunningTaskInstanceStatusEnum.Running:
                    return "Running";
                case LRTM.LongRunningTaskInstanceStatusEnum.Scheduled:
                    return "Scheduled";
            }
        }
    }


    // register controller
    angular.module('Sustainalytics.LRTMTest').controller('LongRunningTaskManagerTestController', LongRunningTaskManagerTestController);
} 